import socket, sys, time

PMS_HOST = '127.0.0.1'
PMS_PORT = 8989

REGION_MAX = 17
AGE_MAX = 7
GENDER_MAX = 2

MSG_END = '\r\n\0'
MENU={}

class CmdMenu:
	def __init__(self):
		global MENU
		MENU['0']=('LIST_GS',self.menu00)
		MENU['1']=('REG_GS',self.menu01)
		MENU['2']=('START_GS',self.menu02)
		MENU['3']=('STOP_GS',self.menu03)
		MENU['4']=('TEST_GS',self.menu04)
		MENU['5']=('GET_PERF_GS',self.menu05)
		MENU['6']=('GET_STAT_GS',self.menu06)
		MENU['7']=('NOTI_GS',self.menu07)
		MENU['99']=('Exit',sys.exit)
		self.m_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		self.m_sock.connect((PMS_HOST,PMS_PORT))
	
	def __del__(self):
		self.m_sock.close()	
	
	def menu00(self):
		msg = 'LISTHA|'
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(1024)
		print '=========== HA list ============='
		for ha in resMsg.split('|')[1:]:
			#GSID,STATUS,IP,PORT,PATH,CNF,GSINFO
			tempL = ha.split(',')
			print "%-4s %-6s %-15s %-6s %s ,%s ,%s" % ( tempL[0],tempL[1],tempL[2],tempL[3],tempL[4],tempL[5],tempL[6])
		
	def menu01(self): 
		#gsID,path,ip,port,config	
		gsID = raw_input("GSID:")
		path = raw_input("EXE PATH(FULL PATH):")
		start = raw_input("start method:")
		ip = raw_input("HA IP:")
		port = raw_input("HA PORT:")
		config = raw_input("CNFG FILE:")
		
		msg = 'REGHA|%s|%s|%s|%s|%s|%s' % (gsID,path,start,ip,port,config)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(1024)
		print '=========== HA Register ============='		
		print resMsg

	def menu02(self):
		gsID = raw_input("GSID:")
		msg = 'STARTGS|%s' % (gsID)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(1024)
		print '=========== START GS ============='
		print resMsg

	def menu03(self):
		gsID = raw_input("GSID:")	
		msg = 'STOPGS|%s' % (gsID)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(1024)
		print '=========== STOP GS ============='
		print resMsg
		
	def menu04(self):
		gsID = raw_input("GSID:")
		msg = 'TESTGS|%s' % (gsID)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		
	def menu05(self):
		gsID = raw_input("GSID:")
		msg = 'PERFGS|%s' % (gsID)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(1024)
		print '=========== PERF GS(%s) =============' % (gsID)
		print resMsg	
		
	def menu06(self):
		gsID = raw_input("GSID:")
		msg = 'STATGS|%s' % (gsID)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		resMsg = self.m_sock.recv(4096)
		print '=========== STAT GS(%s) =============' % (gsID)
		self.print_stat(resMsg)
		
	def menu07(self):
		gsID = raw_input("GSID:")
		noti_msg = raw_input("NOTI:")		
		msg = 'NOTIGS|%s|%s' % (gsID,noti_msg)
		self.m_sock.send(msg.replace('|','%')+MSG_END)
		
	def print_stat(self,msg):
		#Area,age0(M/F),age1(M/F),age2(M/F),age3(M/F),age4(M/F),age5(M/F),age6(M/F),age7(M/F)
		statL = []
		for x in range(0,REGION_MAX):
			ageL = []
			for y in range(0,AGE_MAX):
				genderL = []
				for z in range(0,GENDER_MAX):
					genderL.append(0)
				ageL.append(genderL)
			statL.append(ageL)
			
		tempL = msg.split('|')
		if len(tempL) >= 2:
			print 'Curent Users: %s' % (tempL[1])
			for data in tempL[2:]:
				items = data.split(',')
				area = int(items[0])
				age = int(items[1])
				gender = int(items[2])
				value = int(items[3])
				statL[area][age][gender] = value
			print 'Area,age0(M/F),age1(M/F),age2(M/F),age3(M/F),age4(M/F),age5(M/F),age6(M/F),age7(M/F)'
			for x in range(0,REGION_MAX):
				areaL = []
				areaL.append(str(x))
				for y in range(0,AGE_MAX):
					areaL.append('%d/%d' % (statL[x][y][0],statL[x][y][1]) )
				print areaL
		else:
			print msg

	def print_menu(self):
		print "========= Menu ============="
		items = MENU.items()
		items.sort()
		for key,value in items:
			print "%-2s : %s " % ( key,value[0])
		print "============================"
		return raw_input("choice :")
	
if __name__ == '__main__':
	cmdMenu = CmdMenu()
	while True :
		choice = cmdMenu.print_menu() 
		if choice in MENU.keys():
			MENU[choice][1]()
		else:
			print 'Input Error! Please input correctly!!'
